from requests import session


# 高内聚低耦合: 一个函数或者一个类中只做一件事


class HttpRequest:
    """操作 HTTP 请求和响应"""
    def __init__(self, domain: str, headers: dict = None):
        self.domain = domain
        self.headers = headers if headers is not None else {}
        self.session = session()

    def get(self, api: str, params: dict = None):
        """
        https get 请求
        :param api: 接口名
        :param params: url 参数
        :return:
        """
        r = self.session.get(f"https://{self.domain}{api}", params=params, headers=self.headers)
        return r.json()

    def post(self, api: str, body: dict, params: dict = None):
        """
        https post 请求
        :param api: 接口名
        :param body: body raw
        :param params: url 参数
        :return:
        """
        r = self.session.post(f"https://{self.domain}{api}", json=body, params=params, headers=self.headers)
        return r.json()


if __name__ == "__main__":
    http_request = HttpRequest("qyapi.weixin.qq.com")
    result = http_request.get("/cgi-bin/gettoken", {
        "corpid": "ww183fe17a0f308505",
        "corpsecret": "uldHoRf7KmFs-fNloZ8hB40fgXQR1B8FZwn9UM3gArk"
    })
    print(result)
